<?php
/**
 * Class that operate on table 'payment_updates'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class PaymentUpdatesMySqlDAO implements PaymentUpdatesDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return PaymentUpdatesMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM payment_updates WHERE update_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM payment_updates';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function updates_list($paymentUpdate){
		$sql = 'SELECT pu.updated_date,au.first_name,au.last_name from `payment_updates` pu left join admin_users au on pu.admin_id=au.admin_id WHERE payment_id = "'.$paymentUpdate->payment_id.'"';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM payment_updates ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param paymentUpdate primary key
 	 */
	public function delete($update_id){
		$sql = 'DELETE FROM payment_updates WHERE update_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($update_id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param PaymentUpdatesMySql paymentUpdate
 	 */
	public function insert($paymentUpdate){
		$sql = 'INSERT INTO payment_updates (payment_id, admin_id, created_date, updated_date) VALUES ("'.$paymentUpdate->payment_id.'", "'.$paymentUpdate->admin_id.'", "'.$paymentUpdate->created_date.'", "'.$paymentUpdate->updated_date.'")';
		$sqlQuery = new SqlQuery($sql);
		$id = $this->executeInsert($sqlQuery);	
		$paymentUpdate->update_id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param PaymentUpdatesMySql paymentUpdate
 	 */
	public function update($paymentUpdate){
		$sql = 'UPDATE payment_updates SET payment_id = ?, admin_id = ?, status = ?, created_date = ?, updated_date = ? WHERE update_id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($paymentUpdate->paymentId);
		$sqlQuery->setNumber($paymentUpdate->adminId);
		$sqlQuery->setNumber($paymentUpdate->status);
		$sqlQuery->set($paymentUpdate->createdDate);
		$sqlQuery->set($paymentUpdate->updatedDate);

		$sqlQuery->setNumber($paymentUpdate->updateId);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM payment_updates';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByPaymentId($value){
		$sql = 'SELECT * FROM payment_updates WHERE payment_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByAdminId($value){
		$sql = 'SELECT * FROM payment_updates WHERE admin_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM payment_updates WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM payment_updates WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM payment_updates WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByPaymentId($value){
		$sql = 'DELETE FROM payment_updates WHERE payment_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByAdminId($value){
		$sql = 'DELETE FROM payment_updates WHERE admin_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM payment_updates WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM payment_updates WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM payment_updates WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return PaymentUpdatesMySql 
	 */
	protected function readRow($row){
		$paymentUpdate = new PaymentUpdate();
		
		$paymentUpdate->update_id = $row['update_id'];
		$paymentUpdate->payment_id = $row['payment_id'];
		$paymentUpdate->admin_id = $row['admin_id'];
		$paymentUpdate->status = $row['status'];
		$paymentUpdate->first_name = $row['first_name'];
		$paymentUpdate->last_name = $row['last_name'];
		$paymentUpdate->created_date = $row['created_date'];
		$paymentUpdate->updated_date = $row['updated_date'];

		return $paymentUpdate;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return PaymentUpdatesMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>